package cn.taobaoke.restful.service;

import cn.taobaoke.restful.domain.StatisticsRepository;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangzuo
 * @since 2017-03-27
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class StatisticsService {


    public Map<String, Object> staticsAdminToday(String domain, Long domainId) {
        String start = DateTime.now().toString("yyyy-MM-dd");
        String end = start;

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("todayOrderCount", 0);
        resultMap.put("todayIncome", 0);
        resultMap.put("todayMemberCount", 0);
        resultMap.put("todayAgentCount", 0);

        List<Map> mapList = queryAdminOrderCountAndIncome(start, end, domain);
        if (mapList != null && mapList.size() > 0) {
            resultMap.put("todayOrderCount", mapList.get(0).get("total"));
            resultMap.put("todayIncome", mapList.get(0).get("income"));
        }

        mapList = queryAdminAgentCount(start, end, domainId);
        if (mapList != null && mapList.size() > 0) {
            resultMap.put("todayAgentCount", mapList.get(0).get("count"));
        }

        mapList = queryAdminMemberCount(start, end, domainId);
        if (mapList != null && mapList.size() > 0) {
            resultMap.put("todayMemberCount", mapList.get(0).get("count"));
        }
        return resultMap;
    }


    public Map<String, Object> staticsAgentToday(Long agentId, String domain, Long domainId) {
        String start = DateTime.now().toString("yyyy-MM-dd");
        String end = start;

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("todayOrderCount", 0);
        resultMap.put("todayIncome", 0);
        resultMap.put("todayMemberCount", 0);
        resultMap.put("todayAgentCount", 0);

        List<Map> mapList = queryAgentOrderCountAndIncome(agentId, start, end, domain);
        if (mapList != null && mapList.size() > 0) {
            resultMap.put("todayOrderCount", mapList.get(0).get("total"));
            resultMap.put("todayIncome", mapList.get(0).get("income"));
        }

        mapList = queryAgentSubAgentCount(agentId, start, end, domainId);
        if (mapList != null && mapList.size() > 0) {
            resultMap.put("todayAgentCount", mapList.get(0).get("count"));
        }

        mapList = queryAgentMemberCount(agentId, start, end, domainId);
        if (mapList != null && mapList.size() > 0) {
            resultMap.put("todayMemberCount", mapList.get(0).get("count"));
        }
        return resultMap;
    }


    public List<Map> queryAdminSubAgentIncome(String keyword, String start, String end, Integer page, Integer size, String domain, Long domainId) {
        return statisticsRepository.queryAdminSubAgentIncome(keyword, start, end, page, size, domain, domainId);
    }

    public List<Map> queryAgentSubAgentIncome(String keyword, Long agent1Id, String start, String end, Integer page, Integer size, String domain, Long domainId) {
        return statisticsRepository.queryAgentSubAgentIncome(keyword, agent1Id, start, end, page, size, domain, domainId);
    }

    // 统计管理员订单数量和收入
    public List<Map> queryAdminOrderCountAndIncome(String start, String end, String domain) {
        return statisticsRepository.queryAdminOrderAndIncome(start, end, domain);
    }

    // 统计代理的订单数量和收入
    public List<Map> queryAgentOrderCountAndIncome(Long agentId, String start, String end, String domain) {
        return statisticsRepository.queryAgentOrderAndIncome(agentId, start, end, domain);
    }

    // 统计管理员下面代理的发展数量
    public List<Map> queryAdminAgentCount(String start, String end, Long domainId) {
        return statisticsRepository.queryAdminSubAgent(start, end, domainId);
    }

    // 统计代理的发展数量
    public List<Map> queryAgentSubAgentCount(Long agentId, String start, String end, Long domainId) {
        return statisticsRepository.queryAgentSubAgent(agentId, start, end, domainId);
    }

    // 统计会员发展数量
    public List<Map> queryAdminMemberCount(String start, String end, Long domainId) {
        return statisticsRepository.queryAdminMember(start, end, domainId);
    }

    // 统计代理的发展数量
    public List<Map> queryAgentMemberCount(Long agentId, String start, String end, Long domainId) {
        return statisticsRepository.queryAgentMember(agentId, start, end, domainId);
    }

    public Long countSubAgent(String keyword, Long agent, Long domainId) {
        return statisticsRepository.countSubAgent(keyword, agent, domainId);
    }

    public Long countAgent(String keyword, Long domainId) {
        return statisticsRepository.countAgent(keyword, domainId);
    }

    private final @NonNull
    StatisticsRepository statisticsRepository;
}
